import Vue from 'vue'
import Router from 'vue-router'
import Home from '../components/Home'
import MyRepairs from '../components/MyRepairs'
import DoRepairs from '../components/DoRepairs'
import User from '../components/User'
import DoAdvise from '../components/DoAdvise'
import RepairSquare from '../components/RepairSquare'
import MyAdvise from '../components/MyAdvises'
import DoSetting from '../components/DoSetting'
import RepairShow from '../components/RepairShow'
import store from '../store/store'

Vue.use(Router);

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'home', // 主页
      component: Home,
      meta: {
        KeepAlive: true
      }
    },
    {
      path: '/repairs',  // 我的报修
      name: 'repairs',
      component: MyRepairs,
      meta: {
        auth: true
      }
    },
    {
      path: '/user', // 个人中心
      name: 'user',
      component: User
    },
    {
      path: '/dorepairs', // 进行报修
      name: 'DoRepairs',
      component: DoRepairs,
      meta: {
        auth: true
      }
    },
    {
      path: '/doadvise', // 提建议
      name: 'DoAdvise',
      component: DoAdvise,
      meta: {
        auth: true
      }
    },
    {
      path: '/dosetting', // 设置账号信息
      name: 'DoSetting',
      component: DoSetting,
      meta: {
        auth: true
      }
    },
    {
      path: '/show',  // 显示报修详情
      name: 'Show',
      component: RepairShow
    },
    {
      path: '/resquare', // 报修广场
      name: 'RepairSquare',
      component: RepairSquare,
      meta: {
        auth: true
      }
    },
    {
      path: '/myadvise', // 我的建议
      name: 'MyAdvise',
      component: MyAdvise,
      meta: {
        auth: true
      }
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.meta.auth) { // 需要进行登录验证
    store.dispatch('authLogin').then((a) => {
      a ? next() : next('/')
    })
  } else {
    next()
  }
});

export default router
